package com.yx.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.anno.LogOperation;
import com.yx.pojo.Merchant;
import com.yx.pojo.MerchantVO;
import com.yx.pojo.PageDTO;
import com.yx.pojo.Result;
import com.yx.service.MerchantService;
import com.yx.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j//日志信息
@RequestMapping("/merchants")
@RestController
public class MerchantController {
    @Autowired
    private MerchantService merchantService;

    /***
     * 分页查询商户信息
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public Result<PageDTO<MerchantVO>> pageQuery( MerchantVO merchantVO,
                            @PathVariable("pageNum") int pageNum,
                            @PathVariable("pageSize") int pageSize){
        log.info("分页查询商户全部信息");
        PageDTO<MerchantVO> page = merchantService.pageQuery(merchantVO,pageNum,pageSize);
        return ResultUtils.success(page);
    }
    /**
     * 新增部门
     */
    @LogOperation
    @PostMapping()
    public Result add(@RequestBody Merchant merchant){
        log.info("新增商户为：{}",merchant.getMerchantName());
        merchantService.save(merchant);
        return ResultUtils.success();
    }

    /***
     * 删除部门
     */
    @LogOperation
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable List id) throws Exception {//路径参数
        log.info("根据id删除部门：{}",id);
        merchantService.removeBatchByIds(id);
        return ResultUtils.success();
    }

    /**
     * 根据id查询
     * @return
     */
    @GetMapping("/{id}")
    public Result selectByid(@PathVariable Integer id){
        log.info("根据id查询：{}",id);
        Merchant merchant = merchantService.getById(id);
        return ResultUtils.success(merchant);
    }

    /**
     * 修改部门信息
     * @return
     */
    @LogOperation
    @PutMapping
    public Result update(@RequestBody Merchant merchant){
        merchantService.update(merchant);
        return ResultUtils.success();
    }
}

